package org.smartercontext.local.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.smartercontext.contextmger.contextsphere.ContextManagerController;
import org.smartercontext.contextmger.utilities.GetProducts;

import com.hp.hpl.jena.rdf.model.Model;

/**
 * Servlet implementation class DeleteProduct
 */
@WebServlet("/DeleteProduct")
public class DeleteProduct extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteProduct() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
    	if(request.getQueryString()!=null){
    		HttpSession session = request.getSession();
    		String username = session.getAttribute("username").toString();
    		if(username!=null){
    			String nameToDelete = request.getParameter("nameToDelete");
    			String type = request.getParameter("type");
    			GetProducts gp = new GetProducts();
    			try {
					Model usermodel = ContextManagerController.getUsermodel(username);
					Model usermodelU = gp.deleteProductToUserModel(usermodel, username, nameToDelete, type);
					ContextManagerController.integrateContext(usermodelU, username);
					response.sendRedirect("manageContext_Products.jsp");
				} catch (Exception e) {
					response.sendRedirect("manageContext_Products.jsp?Error=2");
					e.printStackTrace();
				}
    		}
    	}
    	
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
